﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using LyyCMS.Core.Models;

namespace LyyCMS.Core
{
    public class MySqlEntities : DbContext
    {
        public virtual DbSet<Blog> Blogs { get; set; }

        public virtual DbSet<Comments> Comments { get; set; }

        public virtual DbSet<User> Users { get; set; }

        //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        //{
        //    base.OnConfiguring(optionsBuilder);
        //    optionsBuilder.UseMySql("Server=127.0.0.1;database=LyyCMS;uid=root;password=root");//配置连接字符串
        //}

        public MySqlEntities(DbContextOptions<MySqlEntities> dbContextOptions) : base(dbContextOptions)
        {

        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Blog>(entity =>
            {
                entity.Property(e => e.Title).IsRequired();
            });

            //设置一对多
            modelBuilder.Entity<Comments>(entity =>
            {
                entity.HasOne(d => d.Blog)
                .WithMany(p => p.Comments)
                .HasForeignKey("BlogId");
            });

            //User 默认数据
            modelBuilder.Entity<User>().HasData(
                new User { Id = 1, UserName = "Admin", UserPass = "Admin", Crete_At = DateTime.Now, UserState = UserState.Active, UserType = UserType.Admin }
                );


            //Blog种子数据
            modelBuilder.Entity<Blog>().HasData(
                new Blog { Id = 1, UserId = 1, Title = "net core", Content = "net core need", Category = BlogCategory.CSharp, Create_At = DateTime.Now },
                new Blog { Id = 2, UserId = 1, Title = "ef core", Content = "ef core need", Category = BlogCategory.MSSQL, Create_At = DateTime.Now }

            );

            //设置一对多
            modelBuilder.Entity<Comments>(entity =>
            {
                entity.HasOne(d => d.Blog)
                .WithMany(p => p.Comments)
                .HasForeignKey("BlogId");
            });

            //为Comment 添加种子数据
            modelBuilder.Entity<Comments>().HasData(
                new Comments() { BlogId = 1, Id = 1, Title = "Net core is good", Comment = "我不会用", Create_At = DateTime.Now },
                new Comments() { BlogId = 1, Id = 2, Title = "Net core is not good", Comment = "我会用", Create_At = DateTime.Now },
                new Comments() { BlogId = 2, Id = 3, Title = "ef core is good", Comment = "我不会用11", Create_At = DateTime.Now },
                new Comments() { BlogId = 2, Id = 4, Title = "ef core is not good", Comment = "我会用22", Create_At = DateTime.Now }
                );

        }


        //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        //{
        //    base.OnConfiguring(optionsBuilder);
        //    optionsBuilder.UseMySQL("Server=127.0.0.1;database=testdb;uid=root");//配置连接字符串
        //}
        //protected override void OnModelCreating(ModelBuilder modelBuilder)
        //{
        //    base.OnModelCreating(modelBuilder);
        //    var etPerson = modelBuilder.Entity<Person>().ToTable("t_persons");
        //    etPerson.Property(e => e.Age).IsRequired();//不写IsRequired,默认为可空
        //    etPerson.Property(e => e.Gender).IsRequired();
        //    etPerson.Property(e => e.Name).HasMaxLength(20).IsRequired();
        //}

    }
}
